package top.tttfu.dao;


import top.tttfu.entity.User;
import top.tttfu.util.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements IUserDao {

    @Override
    public User selectUserByAccount(Connection con, String account) {
        String sql = "select * from user where accout = ?";
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = con.prepareStatement(sql);
            User user = new User();
            pStatement.setString(1, account);
            rs = pStatement.executeQuery();
            if (rs.next()) {
                user.setAccount(rs.getString("accout"));
                user.setPasswd(rs.getString("pwd"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close(null, pStatement, rs);
        }
        return null;
    }

    @Override
    public Boolean addUser(Connection con, User user) {
        String sql = "insert into user (accout,pwd) values(?,?)";
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = con.prepareStatement(sql);
            pStatement.setString(1, user.getAccount());
            pStatement.setString(2,user.getPasswd());
            pStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            return  false;
        }finally {
            JDBC.close(null, pStatement, rs);
        }
        return true;
    }
}
